package com.wzy.spring2.dao;

import com.wzy.spring2.model.QueryCondition;
import com.wzy.spring2.model.WebUser;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @description:
 * @author: weizhengyun
 * @time: 2023/1/15-14:28
 **/

@Mapper
public interface WebUserDao {

    String TABLE_NAME = "wzy_account_info";

    String FIELDS = "name,account,password,ext";

    @SelectProvider(type = WebUserSqlBuilder.class, method = "toSql")
    @Results({
            @Result(column = "name", property = "webName")
    })
    List<WebUser> select(@Param("qc") QueryCondition qc);


    @Insert({
            "INSERT INTO" +
            TABLE_NAME +
            "(" + FIELDS + ") VALUES" +
            "(#{user.webName}, #{user.account}, #{user.password}, #{user.ext})"
    })
    @Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
    int insert(@Param("user") WebUser user);


    class WebUserSqlBuilder {

        public static String toSql(@Param("qc") QueryCondition qc) {
            StringBuilder sb = new StringBuilder("SELECT ")
                    .append(FIELDS)
                    .append(" FROM ").append(TABLE_NAME)
                    .append(" WHERE 1=1");
            if (StringUtils.isNotEmpty(qc.getWebName())) {
                sb.append(" AND name LIKE ").append("CONCAT('%',#{qc.webName},'%')");

            }
            return sb.toString();
        }

    }

}
